package com.appsindy.reserv.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.action.AppsindyBaseAction;
import com.appsindy.common.action.IManageTab;
import com.appsindy.common.util.StringUtils;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.bo.HtGlobalListValue;
import com.appsindy.reserv.bo.HtGroup;
import com.appsindy.reserv.dao.GlobalListValueDAO;
import com.appsindy.reserv.dao.GroupDAO;
import com.appsindy.reserv.service.ReservationServices;

public class MasterGroupAction extends AppsindyBaseAction implements IManageTab {
	
	private String groupCodeStr;
	private String groupNameStr;
	private String cardNumStr;
	private String enableFlag;
	
	private List<HtGroup> groupList;
	
	private GroupDAO groupDAO;
	private GlobalListValueDAO globalListValueDAO;
	private ReservationServices reservationServices;
	
	private HtGroup htGroup;

	public String execute() {
		log.info("Init Master Group ...");
		
		List<HtGlobalListValue> groupTypes = getGlobalListValueDAO().getGroupTypes();
		if(groupTypes == null){
			groupTypes = new ArrayList<HtGlobalListValue>();
		}
		getSession().put("S_GROUP_TYPES", groupTypes);
		log.info("Init S_GROUP_TYPES ... " + groupTypes.size() + " records.");

		List<HtGlobalListValue> cardTypes = getGlobalListValueDAO().getCardTypes();
		if(cardTypes == null){
			cardTypes = new ArrayList<HtGlobalListValue>();
		}
		getSession().put("S_CARD_TYPES", cardTypes);
		log.info("Init S_CARD_TYPES ... " + cardTypes.size() + " records.");
		
		//setMenuLinkList(getAuthorizationServices().getMenuLinkByLevel(1));
		getSession().put(S_TAB_CODE, MNG_GROUP);
		return SUCCESS;
	}
	
	public String popupGroup(){
		List<HtGlobalListValue> groupTypes = getGlobalListValueDAO().getGroupTypes();
		if(groupTypes == null){
			groupTypes = new ArrayList<HtGlobalListValue>();
		}
		getSession().put("S_GROUP_TYPES", groupTypes);
		log.info("[popupGroup] S_GROUP_TYPES ... " + groupTypes.size() + " records.");
		
		Map<String, Object> params = new HashMap<String, Object>();   
		params.put("groupCode", getGroupCodeStr());
		params.put("groupName", getGroupNameStr());
		params.put("cardNum", getCardNumStr());
		setGroupList(getGroupDAO().searchGroup(params));
		return "showGroups";
	}
	
	public String savePopup() {
		log.info("Save HtGroup getGroupId ... " + htGroup.getGroupId());
		if(htGroup.getGroupId() < 1){
			try{
				htGroup.setOrganizationId(1);
				getGroupDAO().insertGroupPopup(htGroup);
				log.info("Insert Success");
			} catch(Exception e){
				e.printStackTrace();
			}
		} else{
			HtGroup grp = getGroupDAO().selectGroupById(htGroup.getGroupId());
			grp.setGroupCode(htGroup.getGroupCode());
			grp.setGroupName(htGroup.getGroupName());
			grp.setGroupTypeId(htGroup.getGroupTypeId());
			getGroupDAO().updateGroup(grp);
			log.info("Update Success");
		}
		
		List<Combo> groupPopL = getReservationServices().getGroupPopupCombos();
		setContextAttribute("CTX_GROUP_POPUP_LIST", groupPopL);
		return popupGroup();
	}
	
	public String save() {
		log.info("Save HtGroup getGroupId ... " + htGroup.getGroupId());
		if(htGroup.getGroupId() < 1){
			try{
				getGroupDAO().insertGroup(htGroup);
				log.info("Insert Success");
			} catch(Exception e){
				e.printStackTrace();
			}
		} else{
			getGroupDAO().updateGroup(htGroup);
			log.info("Update Success");
		}
		StringBuilder jsonResult = new StringBuilder("{");
		jsonResult.append("'status':1");
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public String delete() {
		log.info("Delete HtGroup Id = " + htGroup.getGroupId());
		StringBuilder jsonResult = new StringBuilder("{");
		if(htGroup.getGroupId() > 0){
			getGroupDAO().deleteGroup(htGroup.getGroupId());
			log.info("Delete Success");
			jsonResult.append("'status':1");
		} else{
			log.info("Delete Failed");
			jsonResult.append("'status':0");
		}
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public List<HtGroup> getTableData() {
		log.info("Init HtGroup TableData ... " + getGroupCodeStr() + ", " + getGroupNameStr());
		List<HtGroup> resultList = new ArrayList<HtGroup>();
		if(StringUtils.isEmpty(getGroupCodeStr()) && StringUtils.isEmpty(getGroupNameStr())
				&& StringUtils.isEmpty(getCardNumStr())){
			Map<String, Object> params = new HashMap<String, Object>();   
			params.put("groupCode", getGroupCodeStr());
			params.put("groupName", getGroupNameStr());
			params.put("cardNum", getCardNumStr());
			List<HtGroup> groupList = getGroupDAO().searchGroup(params);
			if(groupList != null){
				/*java.util.Iterator<HtGroup> list = groupList.iterator();
				while(list.hasNext()){
					HtGroup ht = list.next();
					HtGroup grp = new HtGroup();
					grp.setGroupId(ht.getGroupId());
					grp.setGroupCode(ht.getGroupCode());
					grp.setGroupName(ht.getGroupName());
					grp.setGroupTypeId(ht.getGroupTypeId());
					grp.setEnableFlag(ht.getEnableFlag());
					grp.setCardNum(ht.getCardNum());
					grp.setCardTypeId(ht.getCardTypeId());
					grp.setOrganizationId(ht.getOrganizationId());
					//grp.setStartDate(DateUtils.trimDate(ht.getStartDate()));
					//grp.setEndDate(DateUtils.trimDate(ht.getEndDate()));
					resultList.add(grp);
				}*/
				resultList = groupList;
			}
		}
		log.info("Init Finished ... " + resultList.size() + " rows");
        return resultList;
    }

	public GroupDAO getGroupDAO() {
		return groupDAO;
	}
	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}
	
	public HtGroup getHtGroup() {
		return htGroup;
	}
	public void setHtGroup(HtGroup htGroup) {
		this.htGroup = htGroup;
	}

	public void setGroupCodeStr(String groupCodeStr) {
		this.groupCodeStr = groupCodeStr;
	}
	public String getGroupCodeStr() {
		if(groupCodeStr == null){
			return EMPTY_STRING;
		}
		return groupCodeStr;
	}

	public void setGroupNameStr(String groupNameStr) {
		this.groupNameStr = groupNameStr;
	}
	public String getGroupNameStr() {
		if(groupNameStr == null){
			return EMPTY_STRING;
		}
		return groupNameStr;
	}

	public void setCardNumStr(String cardNumStr) {
		this.cardNumStr = cardNumStr;
	}
	public String getCardNumStr() {
		if(cardNumStr == null){
			return EMPTY_STRING;
		}
		return cardNumStr;
	}
	
	public void setEnableFlag(String enableFlag) {
		this.enableFlag = enableFlag;
	}
	public String getEnableFlag() {
		if(enableFlag == null){
			return EMPTY_STRING;
		}
		return enableFlag;
	}
	
	public void setGlobalListValueDAO(GlobalListValueDAO globalListValueDAO) {
		this.globalListValueDAO = globalListValueDAO;
	}
	public GlobalListValueDAO getGlobalListValueDAO() {
		return globalListValueDAO;
	}
	
	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}
	public ReservationServices getReservationServices() {
		return reservationServices;
	}

	public List<HtGroup> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<HtGroup> groupList) {
		this.groupList = groupList;
	}
	
}
